package com.yijiaganxi.product.service.impl.product;

import com.yijiaganxi.product.dao.product.YjgxProductMapper;
import com.yijiaganxi.product.openFeign.TestOpenFeign;
import com.yijiaganxi.product.service.product.YjgxProductService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yijiaganxi.common.entity.banner.YjgxBanner;
import org.yijiaganxi.common.entity.product.YjgxProduct;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * 商品管理 服务实现类
 * </p>
 *
 * @author zhangguowei123
 * @since 2020-08-29
 */
@Service
public class YjgxProductServiceImpl extends ServiceImpl<YjgxProductMapper, YjgxProduct> implements YjgxProductService {

    @Autowired
    private TestOpenFeign testOpenFeign;
    @Autowired
    private YjgxProductService productService;


    @GlobalTransactional(name = "test1",rollbackFor = Exception.class)
    @Override
    public Object test(Integer i) {

        YjgxProduct product = YjgxProduct.builder()
                .categoryId(111111111l)
                .createDate(new Date())
                .createUser("zhangsan")
                .name("测试分布式事物1name")
                .memberPrice(new BigDecimal(1))
                .detail(" detail  detail ")
                .price(new BigDecimal(1))
                .productImg("producyImAGES")
                .productLabel("productLabel")
                .status(1)
                .weight(new BigDecimal(1)).build();

        System.out.println("全局事物ID "+ RootContext.getXID());
        YjgxBanner banner=YjgxBanner.builder()
                .createDate(new Date())
                .imgUrl("imgUrl")
                .linkUrl("linkUrl")
                .location(1)
                .modifyDate(new Date())
                .nam("testBanner事物1")
                .sort(1)
                .id(111l)
                .status(1).build();
        testOpenFeign.addBanner(banner);

        YjgxBanner banner2=YjgxBanner.builder()
                .createDate(new Date())
                .imgUrl("imgUrl")
                .linkUrl("linkUrl")
                .location(1)
                .modifyDate(new Date())
                .nam("testBanner事物2")
                .sort(3)
                .id(222l)
                .status(1).build();
        testOpenFeign.addBanner(banner2);
        productService.insert(product);
       if(i==1){
           throw  new RuntimeException("项目出错了");
       }
        return "111";
    }
}
